package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.config.SystemConfig;
import com.neu.doc.AdminConfigController_express;
import com.neu.doc.AdminConfigController_mall;
import com.neu.doc.AdminConfigController_wx;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAdmin;
import com.neu.domain.ShoppingSystemExample;
import com.neu.service.ShoppingSystemConfigService;
import com.neu.util.JacksonUtil;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;
@RequestMapping("/admin/config")
@RestController
@Validated
public class AdminConfigController {
    private final Log logger = LogFactory.getLog(AdminConfigController.class);
    @Autowired
    private ShoppingSystemConfigService shoppingSystemConfigService;
    
    @RequiresPermissions("admin:config:mall:list")
    @RequiresPermissionsDesc(menu = {"配置管理", "商场配置"}, button = "详情")
//  TODO  
    @GetMapping("/mall")
    public Object listMall() {
        Map<String, String> data = shoppingSystemConfigService.listMail();
        return ResponseUtil.ok(data);
    }

    @ApiOperation(value="编辑商场配置", notes="编辑一条商场配置信息",response = ResponseOK.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopping_mall_adress", value = "商场地址", required = true, dataType = "string"),
            @ApiImplicitParam(name = "shopping_mall_name", value = "商场名称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "shopping_mall_phone", value = "联系电话", required = true, dataType = "string"),
            @ApiImplicitParam(name = "shopping_mall_qq", value = "联系qq", required = true, dataType = "string")
    })
    @RequiresPermissions("admin:config:mall:updateConfigs")
    @RequiresPermissionsDesc(menu = {"配置管理", "商场配置"}, button = "编辑")
//  TODO 
    @PostMapping("/mall")
    public Object updateMall(@RequestBody String body) {
        Map<String, String> data = JacksonUtil.toMap(body);
        shoppingSystemConfigService.updateConfig(data);
        SystemConfig.updateConfigs(data);
        return ResponseUtil.ok();
    }

    @ApiOperation(value="运费配置", notes="运费配置详情",response = AdminConfigController_express.class)
	@RequiresPermissions("admin:config:express:listExpress")
	@RequiresPermissionsDesc(menu = {"配置管理", "运费配置"}, button = "详情")
    @GetMapping("/express")
    public Object listExpress(String orderByClause) {
    	Map<String, String> seList = shoppingSystemConfigService.listExpress();
    	return ResponseUtil.ok(seList);
    }
    
    @ApiOperation(value="编辑运费配置", notes="编辑一条运费配置信息",response = ResponseOK.class)
	  @RequiresPermissions("admin:config:express:updateExpress")
	  @RequiresPermissionsDesc(menu = {"配置管理", "运费配置"}, button = "编辑")
	  @PostMapping("/express")
	  public Object updateExpress(@ApiParam(name="ShoppingConfig對象",value="传入json格式",required=true)
		@RequestBody String body) {
		     Map<String, String> map = JacksonUtil.toMap(body);
	       shoppingSystemConfigService.updateConfig(map);
	       SystemConfig.updateConfigs(map);
	        return ResponseUtil.ok();
	    }
	 
	
	  
//    @RequiresPermissions("admin:config:express:updateConfigs")
//    @RequiresPermissionsDesc(menu = {"配置管理", "运费配置"}, button = "编辑")
//  TODO 

@ApiOperation(value="获取商城首页配置信息分页数据", notes="分页查询商城首页配置信息",response = AdminConfigController_wx.class)
@RequiresPermissions("admin:config:wx:list")
@RequiresPermissionsDesc(menu = {"配置管理", "商城首页配置"}, button = "详情")
@GetMapping("/wx")
public Object listWx() {
    Map<String, String> data = shoppingSystemConfigService.listWx();
    return ResponseUtil.ok(data);
}
  
@RequiresPermissions("admin:config:express:updateWx")
@RequiresPermissionsDesc(menu = {"配置管理", "商城首页配置"}, button = "编辑")
@PostMapping("/wx")
public Object updateWx(@ApiParam(name="ShoppingConfig對象",value="传入json格式",required=true)
	@RequestBody String body) {
	     Map<String, String> map = JacksonUtil.toMap(body);
     shoppingSystemConfigService.updateConfig(map);
     SystemConfig.updateConfigs(map);
      return ResponseUtil.ok();
  }
}
//    @RequiresPermissions("admin:config:wx:updateConfigs")
//    @RequiresPermissionsDesc(menu = {"配置管理", "商城首页配置"}, button = "编辑")
//  TODO 

